package org.link.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.link.bean.Article;
import org.link.service.ArticleService;

import com.alibaba.fastjson.JSON;


/**
 * 
 * project_name:forumweb
 * package_name:org.link.web
 * file_name:ArticleServlet.java
 * @author Link
 * create_date:2015年10月27日 下午4:25:07
 * modified_date:
 * description: 帖子类消息处理
 *
 */
public class ArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArticleService articleService = new ArticleService();
	private HttpSession session = null;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String op = request.getParameter("op");
		if(op.equals("sendArticle")){
			sendArticleServlet(request,response);
		}else if(op.equals("changePage")){
			changeArticlePage(request,response);
		}else if(op.equals("articlePage")){
			articlePage(request,response);
		}else if(op.equals("receiptArticle")){
			addReceiptArticle(request,response);
		}
	}
	
	/**
	 * 
	 * description: 添加回帖
	 * creatTime:2015年11月1日上午12:51:00
	 * @author Link
	 * return type: void
	 * @throws IOException 
	 *
	 */
	private void addReceiptArticle(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		session = request.getSession();
		String content = request.getParameter("content");
		int mainId = Integer.parseInt(request.getParameter("mainId"));
		int userId = Integer.parseInt(request.getParameter("userId"));
		session.setAttribute("mainArticle", articleService.queryMainArticle(mainId));
		session.setAttribute("receiptArticle", articleService.queryAllArticle(content, mainId, userId));
		response.sendRedirect("pages/article.jsp");
	}

	/**
	 * 
	 * description: 帖子信息主页
	 * creatTime:2015年10月31日下午11:55:04
	 * @author Link
	 * return type: void
	 * @throws IOException 
	 *
	 */
	private void articlePage(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		session = request.getSession();
		String id = request.getParameter("id");
		Article mainArticle = articleService.queryMainArticle(Integer.parseInt(id));
		ArrayList<Article> receiptArticle = articleService.queryAllArticle(Integer.parseInt(id));
		session.setAttribute("mainArticle", mainArticle);
		session.setAttribute("receiptArticle", receiptArticle);
		response.sendRedirect("pages/article.jsp");
	}

	/**
	 * 
	 * description:页面切换方法体
	 * 2015年10月29日 上午10:55:19
	 * @author Link
	 * @param request
	 * @param response
	 * @throws IOException 
	 *
	 */
	private void changeArticlePage(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int curPage = Integer.parseInt(request.getParameter("curPage"));
		int rows = 4;
		int totalCount = articleService.articleCount(0);
		if(curPage*rows>totalCount){
			rows = rows-(curPage*rows-totalCount);
		}
		ArrayList<Article> article = articleService.queryAllArticle((curPage-1)*4, rows);
		String articleJSON = JSON.toJSONString(article);
		PrintWriter out = response.getWriter();
		out.print(articleJSON);
	}


	/**
	 * 
	 * description:帖子发送数据库存储
	 * 2015年10月27日 下午5:19:13
	 * @author Link
	 * @param request
	 * @param response
	 * @throws IOException 
	 *
	 */
	private void sendArticleServlet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		session = request.getSession();
		String fatherName = request.getParameter("father");
		String childName = request.getParameter("child");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String email = request.getParameter("email");
		String point = request.getParameter("point");
		
		
		//System.out.println(content);
		//response.sendRedirect("user-index.jsp");
		if(articleService.addMainAticle(fatherName,childName,title,content,email,Integer.parseInt(point))==1){
			int rows = 4;		//显示行数
			int beginIndex = 0;//起始显示索引位置
			int totalPage = 0;//总页数
			int allMainArticCount = articleService.articleCount(0);//主贴子总数
			if(allMainArticCount%rows==0){
				totalPage = allMainArticCount/rows;
			}else{
				totalPage = allMainArticCount/rows+1;
			}
			session.setAttribute("curPage", 1);//当前页数
			session.setAttribute("totalPage", totalPage);//总共页数
			session.setAttribute("mainArticleInfo", articleService.queryAllArticle(beginIndex, rows));//需要显示的帖子数
			response.sendRedirect("user-index.jsp");
		}
		
		
	}
    
}
